package com.chengwei.ch.busdemo.base;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.chengwei.ch.busdemo.R;
import com.gyf.barlibrary.ImmersionBar;

import static com.chengwei.ch.busdemo.APP.app;
import static com.chengwei.ch.busdemo.APP.mScreensaver;

@SuppressLint("Registered")
public class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 内容
     */
    protected ConstraintLayout mainContentView;
    /**
     * 标题栏
     */
    protected View titleView;
    /**
     * 返回按钮
     */
    protected ImageButton btnBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        initImmersionBar();
        app.pushActivity(this);
        mainContentView = findViewById(R.id.mainContentView);
        titleView = findViewById(R.id.title_view);
        btnBack = findViewById(R.id.base_back);
        initClickListener();
    }

    /**
     * 当触摸就会执行此方法
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mScreensaver != null) {
           // mScreensaver.resetTime(); //重置时间
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 当使用键盘就会执行此方法
     */
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mScreensaver != null) {
          //  mScreensaver.resetTime(); //重置时间
        }
        return super.dispatchKeyEvent(event);
    }


    private void initClickListener() {
        btnBack.setOnClickListener(this);
    }

    @SuppressLint("ObsoleteSdkInt")
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initImmersionBar() {
        ImmersionBar
                .with(this)
                .keyboardEnable(true)
                .statusBarDarkFont(true, 0.2f)
                .navigationBarColor(android.R.color.transparent)
                .init();

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(Color.parseColor("#000000"));
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.base_back) {
            Log.d("BaseActivity", "1111111111111111111");
            OnBackClicked(btnBack);
        }
    }

    /**
     * 返回按钮单击事件
     */
    protected void OnBackClicked(View v) {
        finish();
    }

    /**
     * 获取标题
     */
    protected TextView setTitle(String title) {
        TextView tv = findViewById(R.id.title_textView);
        tv.setVisibility(View.VISIBLE);
        tv.setText(title);
        return tv;
    }

    /**
     * 设置页面内容：你用这个来添加你的XML页面内容吧
     */
    protected void setMainView(@LayoutRes int layoutId) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(layoutId, mainContentView, false);
        setMainView(v);
    }

    /**
     * 设置页面内容
     */
    protected void setMainView(View v) {
        mainContentView.addView(v, new LinearLayout.LayoutParams(-1, -1));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        app.popActivity(this);
    }
}
